<?php

namespace apiend\modules\user\controllers;

use Yii;
use apiend\components\Controller;

/**
 * 用户手机号绑定相关
 * MobileController for the `user` module
 */
class MobileController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'untie' => ['DELETE'],
            'binding' => ['POST'],
        ];
    }

    /**
     * 用户手机号解绑
     * @return mixed
     */
    public function actionUntie() {
        $model = new MobileUntieForm();
        $post = Yii::$app->request->post();
        if (!$model->load($post, '')) {
            parent::warning('手机号解绑提交的数据未加载成功');
        }
        $data = $model->save();
        $data['post'] = $post;
        return $data;
        $rules = [
            'password' => 'required|min:6|max:20',
        ];
        $data = Input::only(array_keys($rules));
        $v = validator($data, $rules);
        if ($v->fails()) {
            return ['code' => 418, 'msg' => $v->errors()->first()];
        }
        $user = Auth::user();
        if (!$user->checkPassword(Input::get('password'))) {
            return ['code' => 401, 'msg' => '密码错误'];
        }
        $user->mobile = null;
        if (!$user->save()) {
            logger()->error('手机解绑失败');
            return ['code' => 500, 'msg' => '手机解绑失败'];
        }
        // TODO: 记录日志
        // TODO: 给用户发送提醒短信
        return ['code' => 200, 'msg' => '手机解绑成功'];
        return $this->render('template');
    }

}
